package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Objects;
import java.util.Set;

@Service
public class UserServiceImpl  implements UserService {
   @Autowired
   private UserMapper userMapper;
    @Override
    public User findUserByUsername(String username) {
        //查询用户
        User user = userMapper.findByUsername(username);
        if(Objects.isNull(user)){
            throw new RuntimeException("用户不存在");
        }
        //用户id查相关联的角色
        Set<Role> roleSet  = userMapper.findRoleByUserId(user.getId());
        for (Role role : roleSet) {
            //角色id查询关联权限
            Set<Permission> permissionSet = userMapper.findPermissionByRoleId(role.getId());
            role.setPermissions(permissionSet);
        }

        user.setRoles(roleSet);
        return user;
    }
}
